/*

    gl-web  Reusable web components.
    Copyright (c) 2012-2013 National Marrow Donor Program (NMDP)

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published
    by the Free Software Foundation; either version 3 of the License, or (at
    your option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
    License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library;  if not, write to the Free Software Foundation,
    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.

    > http://www.fsf.org/licensing/licenses/lgpl.html
    > http://www.opensource.org/licenses/lgpl-license.php

*/
package org.immunogenomics.gl.web.dosfilter;

import java.util.concurrent.TimeUnit;

/**
 * Client connection state.
 */
public class ClientConnectionState {

    private final DenialOfServiceConfigMXBean config;
    protected State state;

    private ClientConnectionState(final DenialOfServiceConfigMXBean config) {
        this.config = config;
        state = new FreeHitState();
    }

    public long getLastAccessTime() {
        return state.getLastAccessTime();
    }

    public boolean isBlocked(final long requestTime) {
        state = state.nextState(requestTime, config);
        state.lastAccessTime = requestTime;
        return state.isBlocked();
    }

    public static ClientConnectionState create(final DenialOfServiceConfigMXBean config) {
        return new ClientConnectionState(config);
    }
    
    private static abstract class State {
        protected long lastAccessTime;

        public abstract State nextState(long requestTime, DenialOfServiceConfigMXBean config);
        
        public String toString() {
            return getClass().getSimpleName();
        }

        public long getLastAccessTime() {
            return lastAccessTime;
        }
        
        public boolean isBlocked() {
            return false;
        }
    }
    
    private static class FreeHitState extends State {
        private int hits = 0;

        @Override
        public State nextState(final long requestTime, final DenialOfServiceConfigMXBean config) {
            ++hits;
            if (hits > config.getFreeHitCount()) {
                return new ThrottledState(requestTime);
            }
            return this;
        }
    }
    
    private static class ThrottledState extends State {
        private int hits = 0;
        private final long endThrottleTime;

        public ThrottledState(final long requestTime) {
            // set the time the throttle expires
            this.endThrottleTime = requestTime + TimeUnit.MINUTES.toMillis(1);
        }

        @Override
        public State nextState(final long requestTime, final DenialOfServiceConfigMXBean config) {
            ++hits;
            if (requestTime > endThrottleTime) {
                return new FreeHitState();
            }
            else if (hits > config.getAnonymousHitsPerMinute()) {
                return new BlockedState();
            }
            else {
                // Delay response and continue throttling
                try {
                    Thread.sleep(config.getThrottleDelay());
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return this;
            }
        }
    }
    
    private static class BlockedState extends State {

        @Override
        public State nextState(final long requestTime, final DenialOfServiceConfigMXBean config) {
            return this;
        }

        @Override
        public boolean isBlocked() {
            return true;
        }
    }
}
